package org.jeecg.modules.PriceSystemModule.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.PriceSystemModule.entity.PriceInervalEntity;
import org.jeecg.modules.PriceSystemModule.entity.PriceSystemEntity;
import org.jeecg.modules.PriceSystemModule.mapper.PriceManagementMapper;
import org.jeecg.modules.PriceSystemModule.mapper.TechnologyCostManageMapper;
import org.jeecg.modules.PriceSystemModule.service.TechnologyCostManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TechnologyCostManageServiceImpl implements TechnologyCostManageService {
    @Autowired
    TechnologyCostManageMapper mapper;

    @Autowired
    PriceManagementMapper pricemapper;
    /*
    成本管理中的工艺成本管理查询
     */
    @Override
    public IPage<PriceSystemEntity> getCostManageList(Page<PriceSystemEntity> page, Integer category, Integer businessType, String sysuserId) {
        IPage<PriceSystemEntity> pageList=mapper.getCostManageList(page,category,businessType,sysuserId);
        if (pageList != null)
            for (int i = 0; i < pageList.getRecords().size(); i++) {
                String[] fuleiId = pageList.getRecords().get(i).getTopid().split(",");
                String parentName = "";
                for (int j = 0; j < fuleiId.length; j++) {
                    if (j == 0)
                        parentName = parentName + pricemapper.getpartentNameBytopId(fuleiId[j]);
                    if (j > 0)
                        parentName = parentName + "," + pricemapper.getpartentNameBytopId(fuleiId[j]);
                }
                pageList.getRecords().get(i).setParentName(parentName);
            }
        return pageList;
    }

    /**
     * 查询工厂的价格体系区间
     * @param pricesystemFactoryId
     * @return
     */
    @Override
    public List<PriceInervalEntity> getcostInervallist(String pricesystemFactoryId) {
        return mapper.getcostInervallist(pricesystemFactoryId);
    }

    /**
     * 删除成本阶梯数据
     * @param id
     * @return
     */
    @Override
    public int deletecostInerval(int id) {
        return mapper.deletecostInerval(id);
    }

    /**
     * 查询这个成本区间之前是否已经存在
     * @param pricesystemFactoryId
     * @param minNumbers
     * @param maxNumbers
     * @return
     */
    @Override
    public String costInervaloldcz(String pricesystemFactoryId, int minNumbers, int maxNumbers) {
        return mapper.costInervaloldcz(pricesystemFactoryId, minNumbers, maxNumbers);
    }

    /**
     * 新增一条成本 工厂价格体系区间数据
     * @param entity
     * @return
     */
    @Override
    public int addcostInerval(PriceInervalEntity entity) {
        entity.setCreateTime(new Date());
        return mapper.addcostInerval(entity);
    }

    /**
     * 修改工厂的价格体系表的状态
     * @param pricesystemFactoryId
     */
    @Override
    public void updatestatus(String pricesystemFactoryId) {
         mapper.updatestatus(pricesystemFactoryId);
    }

    /**
     * 通过父级id查询下面的所有工艺
     * @param parentId
     * @return
     */
    @Override
    public List<Map<String, String>> getallsongongyi(String parentId) {
        List<Map<String, String>> list=mapper.getallsongongyi();
        System.out.println("输出看一看这个长度"+list.size());
         if(list!=null){
             for(int i=list.size()-1;i>=0;i--){
                 String[] fuleiId = list.get(i).get("topid").split(",");
                 boolean cunzai=false;
                 //这里循环判断父级id数组里是否存在我们传入的父级id
                 for(int j=0;j<fuleiId.length;j++){
                     if(parentId.equals(fuleiId[j])){
                         cunzai=true;
                     }
                 }
                 //不存在则删掉
                 System.out.println("输出看一看这个时候cunzai是什么"+cunzai);
                 if(cunzai==false){
                     list.remove(i);
                 }
             }
         }
        return list;
    }

    /**
     * 新增成本体系
     * @param gongyi
     * @param category
     * @param sysuserId
     * @return
     */
    @Override
    public int addcostSystem(String gongyi, String category, String sysuserId) {
      int count=  mapper.addcostSystem( gongyi,  category,new Date(),sysuserId,0);
        int count2= mapper.addcostSystem( gongyi,  category,new Date(),sysuserId,1);
        return mapper.addcostSystem( gongyi,  category,new Date(),sysuserId,2);
    }

    /**
    查询这个供应商的这个工艺是否已经设置过
     */
    @Override
    public int getsysgongyicost(String gongyi, String sysuserId) {
        return mapper.getsysgongyicost(gongyi,sysuserId);
    }

    @Override
    public List<PriceInervalEntity> getPriceInervalEntitylist(String pricesystemFactoryId) {
        return mapper.getPriceInervalEntitylist(pricesystemFactoryId) ;
    }
}
